package com.yswee.mallnavi.view.park;

import android.content.Context;
import android.util.AttributeSet;

import com.yswee.framework.data.BaseArrayData;
import com.yswee.framework.data.parser.IParser;
import com.yswee.framework.net.ITaskContext;
import com.yswee.framework.net.http.Callback;
import com.yswee.framework.net.http.Entity;
import com.yswee.framework.utils.StringUtils;
import com.yswee.framework.utils.ViewInject;
import com.yswee.framework.widget.MToast;
import com.yswee.framework.widget.base.WebView;
import com.yswee.framework.widget.loading.LoadingLayout;
import com.yswee.mallnavi.R;
import com.yswee.mallnavi.entity.info.CategoryEntity;
import com.yswee.mallnavi.entity.info.InfoEntity;
import com.yswee.mallnavi.model.info.InfoModel;

import java.util.ArrayList;

public class ParkView extends LoadingLayout {

    @ViewInject
    private WebView uvwebview;

    private InfoModel mInfoModel;

    public ParkView(Context context) {
        super(context);
    }

    public ParkView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.view_info_park;
    }

    @Override
    protected void onBindListener() {
        super.onBindListener();
    }

    @Override
    protected void onApplyData() {
        super.onApplyData();
    }

    @Override
    public void onApplyLoadingData() {
        getDataInfo();
    }

    private void getDataInfo() {
        if(mInfoModel == null) {
            ITaskContext context = mContext instanceof ITaskContext ?
                    (ITaskContext) mContext : null;
            mInfoModel = new InfoModel(context);
        }
        gotoLoading();
        mInfoModel.getCategoryInfo(5, new Callback<CategoryEntity>() {
            @Override
            public void onFinish(Entity<CategoryEntity> entity) {
                IParser<CategoryEntity> parser = entity.getParser();
                final CategoryEntity data = entity.getParsedData();
                if(parser.getIsOk() && data != null) {
                    getListInfo(data.id);
                    return;
                }
                String msg = entity.getMessage();
                if(StringUtils.isEmpty(msg)) {
                    msg = getResources().getString(R.string.park_getdata_fail);
                }
                MToast.showToastMessage(msg);
                gotoError();
            }

            @Override
            public void onError(Entity<CategoryEntity> entity) {
                gotoError();
                if(entity.getEntityStatus() != Entity.NETINVALID) {
                    MToast.showToastMessage(R.string.park_getdata_fail);
                }
            }
        });
    }

    private void getListInfo(String categoryId) {
        if(mInfoModel == null) {
            ITaskContext context = mContext instanceof ITaskContext ?
                    (ITaskContext) mContext : null;
            mInfoModel = new InfoModel(context);
        }
        gotoLoading();
        mInfoModel.getListInfo(categoryId, new Callback<BaseArrayData<InfoEntity>>() {
            @Override
            public void onFinish(Entity<BaseArrayData<InfoEntity>> entity) {
                IParser<BaseArrayData<InfoEntity>> parser = entity.getParser();
                final BaseArrayData<InfoEntity> data = entity.getParsedData();
                if(parser.getIsOk() && data != null && data.getArray() != null) {
                    applyDataInfo(data.getArray());
                    gotoSuccessful();
                    return;
                }
                String msg = entity.getMessage();
                if(StringUtils.isEmpty(msg)) {
                    msg = getResources().getString(R.string.park_getdata_fail);
                }
                MToast.showToastMessage(msg);
                gotoError();
            }

            @Override
            public void onError(Entity<BaseArrayData<InfoEntity>> entity) {
                gotoError();
                if(entity.getEntityStatus() != Entity.NETINVALID) {
                    MToast.showToastMessage(R.string.park_getdata_fail);
                }
            }
        });
    }

    private void applyDataInfo(ArrayList<InfoEntity> array) {
        if (array.size() > 0) {
            String htmlContent = array.get(0).getDescription();
            uvwebview.setVerticalScrollBarEnabled(false);
            uvwebview.setScrollbarFadingEnabled(false);
            uvwebview.loadData(htmlContent, "text/html", "UTF-8");
        }
    }

}
